﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Dao;
using Domain;
using eBay.Service.Core.Soap;
using eBay.Service.Call;
using eBayBus;
using System.Collections;

namespace StampApp
{
    public partial class EbayPage : Form
    {
        public EbayPage()
        {
            InitializeComponent();

            this.gridview_schedule.Columns.Add("index", "id");
            this.gridview_sold.Columns.Add("index", "id");
            this.gridview_active.Columns.Add("index", "id");
            this.gridview_unsold.Columns.Add("index", "id");


            //GridLoad2();
        }

        private void but_revise_Click(object sender, EventArgs e)
        {

        }

        //private void GridLoad2()
        //{

        //    ChaEbayListingDao listingDao = new ChaEbayListingDao();

        //    this.gridview_active.DataSource = new SortableBindingList<ChaEbayListing>(listingDao.LoadAll());

        //    this.gridview_active.Sort(this.gridview_active.Columns["updatedate"], ListSortDirection.Descending);

        //    if (!this.gridview_active.Columns.Contains("index"))
        //    {

        //        this.gridview_active.Columns.Add("index", "id");
        //    }
        //    this.gridview_active.Columns["index"].DisplayIndex = 0;
        //    this.gridview_active.Columns["itemId"].DisplayIndex = 1;


        //}

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {

            foreach (DataGridViewRow row in gridview_active.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }

        }

        private void but_Sync_Click(object sender, EventArgs e)
        {
            but_Sync.Enabled = false;

            try
            {
                gridview_active.DataSource = null;
                gridview_schedule.DataSource = null;
                gridview_sold.DataSource = null;
                gridview_unsold.DataSource = null;

                GetMyeBaySellingCall apicall = new GetMyeBaySellingCall(EbayConst.GetEbayContext(com_EbayAccount.SelectedItem.ToString()));

                apicall.SellingSummary = new ItemListCustomizationType();
                apicall.SellingSummary.Include = true;
                apicall.SellingSummary.IncludeSpecified = true;

                PaginationType pageInfo_active = null;
                pageInfo_active = new PaginationType();
                pageInfo_active.EntriesPerPage = 200;

                PaginationType pageInfo_sold = null;
                pageInfo_sold = new PaginationType();
                pageInfo_sold.EntriesPerPage = 200;

                PaginationType pageInfo_unsold = null;
                pageInfo_unsold = new PaginationType();
                pageInfo_unsold.EntriesPerPage = 200;

                apicall.ActiveList = new ItemListCustomizationType();
                apicall.ActiveList.Sort = ItemSortTypeCodeType.EndTime;
                //apicall.ScheduledList = new ItemListCustomizationType();
                //apicall.ScheduledList.Sort = ItemSortTypeCodeType.EndTime;
                apicall.UnsoldList = new ItemListCustomizationType();
                apicall.UnsoldList.Sort = ItemSortTypeCodeType.EndTime;
                apicall.SoldList = new ItemListCustomizationType();
                apicall.SoldList.Sort = ItemSortTypeCodeType.EndTime;

                //apicall.ApiRequest.DetailLevel = new DetailLevelCodeTypeCollection();
                //apicall.ApiRequest.DetailLevel.Add(DetailLevelCodeType.ReturnSummary);




                //if (pageInfo != null)
                //{
                apicall.ActiveList.Pagination = pageInfo_active;
                apicall.UnsoldList.Pagination = pageInfo_unsold;
                //apicall.ScheduledList.Pagination = pageInfo;
                apicall.SoldList.Pagination = pageInfo_sold;
                //}

                //apicall.GetMyeBaySelling(null, apicall.ActiveList, apicall.SoldList, apicall.UnsoldList, null, null, null, apicall.SellingSummary, true);
                apicall.GetMyeBaySelling();

                ItemTypeCollection unsoldList = new ItemTypeCollection();
                OrderTransactionTypeCollection soldList = new OrderTransactionTypeCollection();
                ItemTypeCollection activeList = new ItemTypeCollection();

                int maxcount_unsold = apicall.UnsoldListReturn == null ? 0 : apicall.UnsoldListReturn.PaginationResult.TotalNumberOfEntries;
                int maxcount_sold = apicall.SoldListReturn == null ? 0 : apicall.SoldListReturn.PaginationResult.TotalNumberOfEntries;
                int maxcount_active = apicall.ActiveListReturn == null ? 0 : apicall.ActiveListReturn.PaginationResult.TotalNumberOfEntries;

                int pageNumber_unsold = (maxcount_unsold % 200) > 0 ? (maxcount_unsold / 200) + 1 : (maxcount_unsold / 200);
                int pageNumber_sold = (maxcount_sold % 200) > 0 ? (maxcount_sold / 200) + 1 : (maxcount_sold / 200);
                int pageNumber_active = (maxcount_active % 200) > 0 ? (maxcount_active / 200) + 1 : (maxcount_active / 200);

                int currentPage_unsold = 2;
                int currentPage_sold = 2;
                int currentPage_active = 2;


                if (maxcount_unsold > 0) { unsoldList.AddRange(apicall.UnsoldListReturn.ItemArray); }
                if( maxcount_sold > 0 ){  soldList.AddRange(apicall.SoldListReturn.OrderTransactionArray);}
                if( maxcount_active>0){ activeList.AddRange(apicall.ActiveListReturn.ItemArray);}

                //while (currentPage_unsold <= pageNumber_unsold)
                //{
                //    apicall.UnsoldList.Pagination.PageNumber = currentPage_unsold;
                //    apicall.GetMyeBaySelling();
                //    unsoldList.AddRange(apicall.UnsoldListReturn.ItemArray);
                //    currentPage_unsold++;
                //}

                while (currentPage_sold <= pageNumber_sold)
                {
                    apicall.SoldList.Pagination.PageNumber = currentPage_sold;
                    apicall.GetMyeBaySelling();
                    soldList.AddRange(apicall.SoldListReturn.OrderTransactionArray);
                    currentPage_sold++;
                }

                while (currentPage_active <= pageNumber_active)
                {
                    apicall.ActiveList.Pagination.PageNumber = currentPage_active;
                    apicall.GetMyeBaySelling();
                    activeList.AddRange(apicall.ActiveListReturn.ItemArray);
                    currentPage_active++;
                }

                //if (apicall.ActiveListReturn.PaginationResult.TotalNumberOfEntries > apicall.SoldListReturn.PaginationResult.TotalNumberOfEntries)
                //{
                //    maxcount = apicall.ActiveListReturn.PaginationResult.TotalNumberOfEntries;
                //}
                //else
                //{
                //    maxcount = apicall.SoldListReturn.PaginationResult.TotalNumberOfEntries;                    
                //}

                //if (maxcount < apicall.UnsoldListReturn.PaginationResult.TotalNumberOfEntries)
                //{
                //    maxcount = apicall.UnsoldListReturn.PaginationResult.TotalNumberOfEntries;
                //}



                DataTable LstActive = new DataTable();

                LstActive.Columns.Add("itemid",typeof(string));
                LstActive.Columns.Add("title", typeof(string));
                LstActive.Columns.Add("currentPrice", typeof(string));
                LstActive.Columns.Add("bidCount", typeof(string));
                LstActive.Columns.Add("endTime", typeof(string));
                LstActive.Columns.Add("sku", typeof(string));

                //DataTable LstScheduled = new DataTable();
                //LstScheduled.Columns.Add("itemid", typeof(string));
                //LstScheduled.Columns.Add("title", typeof(string));
                //LstScheduled.Columns.Add("currentPrice", typeof(string));
                //LstScheduled.Columns.Add("bidCount", typeof(string));
                //LstScheduled.Columns.Add("endTime", typeof(string));

                DataTable LstSold = new DataTable();
                LstSold.Columns.Add("itemid", typeof(string));
                LstSold.Columns.Add("title", typeof(string));
                LstSold.Columns.Add("currentPrice", typeof(string));
                LstSold.Columns.Add("bidCount", typeof(string));
                LstSold.Columns.Add("endTime", typeof(string));
                LstSold.Columns.Add("sku", typeof(string));

                //DataTable LstUnSold = new DataTable();
                //LstUnSold.Columns.Add("itemid", typeof(string));
                //LstUnSold.Columns.Add("title", typeof(string));
                //LstUnSold.Columns.Add("currentPrice", typeof(string));
                //LstUnSold.Columns.Add("bidCount", typeof(string));
                //LstUnSold.Columns.Add("endTime", typeof(string));

                if (apicall.ActiveListReturn != null &&
                    apicall.ActiveListReturn.ItemArray != null &&
                    apicall.ActiveListReturn.ItemArray.Count > 0)
                {
                    foreach (ItemType actitem in activeList)
                    {
                       string[] listparams = new string[6];
                       listparams[0] = actitem.ItemID;
                       listparams[1] = actitem.Title;
                       if (actitem.SellingStatus != null)
                        {
                            listparams[2] = actitem.SellingStatus.CurrentPrice.Value.ToString();
                            listparams[3] = actitem.SellingStatus.BidCount.ToString();
                            listparams[4] = actitem.ListingDetails.EndTime.ToString();
                        }
                       listparams[5] = actitem.SKU;

                        LstActive.Rows.Add(listparams);                       

                    }

                    gridview_active.DataSource = LstActive;
                }
                //if (apicall.ScheduledListReturn != null &&
                //    apicall.ScheduledListReturn.ItemArray != null &&
                //    apicall.ScheduledListReturn.ItemArray.Count > 0)
                //{
                //    foreach (ItemType scheItem in apicall.ScheduledListReturn.ItemArray)
                //    {
                //        string[] listparams = new string[5];
                //        listparams[0] = scheItem.ItemID;
                //        listparams[1] = scheItem.Title;
                //        if (scheItem.SellingStatus != null)
                //        {
                //            listparams[2] = scheItem.SellingStatus.CurrentPrice.Value.ToString();
                //            listparams[3] = scheItem.SellingStatus.BidCount.ToString();
                //            listparams[4] = scheItem.ListingDetails.EndTime.ToString();
                //        }

                //        LstScheduled.Rows.Add(listparams);
                //    }

                //    gridview_schedule.DataSource = LstScheduled;

                //}
                if (apicall.SoldListReturn != null &&
                    apicall.SoldListReturn.OrderTransactionArray != null &&
                    apicall.SoldListReturn.OrderTransactionArray.Count > 0)
                {
                    foreach (OrderTransactionType solditem in soldList)
                    {
                        string[] listparams = new string[6];
                        if (solditem.Transaction != null)
                        {
                            listparams[0] = solditem.Transaction.Item.ItemID;
                            listparams[1] = solditem.Transaction.Item.Title;
                            if (solditem.Transaction.Item.SellingStatus != null)
                            {
                                listparams[2] = solditem.Transaction.Item.SellingStatus.CurrentPrice.Value.ToString();
                                if (solditem.Transaction.Item.SellingStatus.BidCount > 0)
                                    listparams[3] = solditem.Transaction.Item.SellingStatus.BidCount.ToString();
                                if (solditem.Transaction.Item.ListingDetails != null)
                                    listparams[4] = solditem.Transaction.Item.ListingDetails.EndTime.ToString();
                            }

                            listparams[5] = solditem.Transaction.Item.SKU;

                        }


                        LstSold.Rows.Add(listparams);

                    }

                    gridview_sold.DataSource = LstSold;

                }

                //if (apicall.UnsoldListReturn != null &&
                //    apicall.UnsoldListReturn.ItemArray != null &&
                //    apicall.UnsoldListReturn.ItemArray.Count > 0)
                //{
                //    foreach (ItemType unsolditem in unsoldList)
                //    {
                //        string[] listparams = new string[5];
                //        listparams[0] = unsolditem.ItemID;
                //        listparams[1] = unsolditem.Title;
                //        if (unsolditem.SellingStatus != null)
                //        {
                //            listparams[2] = unsolditem.SellingStatus.CurrentPrice.Value.ToString();
                //            listparams[3] = unsolditem.SellingStatus.BidCount.ToString();
                //            listparams[4] = unsolditem.ListingDetails.EndTime.ToString();
                //        }

                //        LstUnSold.Rows.Add(listparams);

                //    }

                //    gridview_unsold.DataSource = LstUnSold;

                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            but_Sync.Enabled = true;

        }

        private void gridview_sold_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in gridview_sold.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void gridview_unsold_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in gridview_unsold.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void gridview_fee_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in gridview_fee.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void but_End_Click(object sender, EventArgs e)
        {


            
        }
    }
}
